package exercise3;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ScoreList {

    List<Student> stuList =new ArrayList<Student>();
    File data =new File("studentlist.txt");
    Random random =new Random();

    int[] sumOfChin=new int[5];
    int[] sumOfMath=new int[5];
    int[] sumOfEng=new int[5];
    int[] sumOfPhy=new int[5];
    int[] sumOfChe=new int[5];
    int[] sumOfSum=new int[5];

    public void SetInfo()throws IOException {
        try {
            if (data.isFile() && data.exists()) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(data));
                BufferedReader bRead = new BufferedReader(read);
                for (int i = 0; i < 5; i++) {
                    Student s = new Student();
                    String line=bRead.readLine();
                    String[] nameOrId = line.split(".");
                    s.setName(nameOrId[0]);
                    s.setId(nameOrId[1]);
                    s.setChinese(getScore());
                    sumOfChin[i]=s.getChinese();
                    s.setMath(getScore());
                    sumOfMath[i]=s.getMath();
                    s.setEng(getScore());
                    sumOfEng[i]=s.getEng();
                    s.setPhy(getScore());
                    sumOfPhy[i]=s.getPhy();
                    s.setChe(getScore());
                    sumOfChe[i]=s.getChe();
                    s.setSum(s.getChinese()+s.getMath()+s.getEng()+s.getPhy()+s.getChe());
                    sumOfSum[i]=s.getSum();
                    stuList.add(s);
                }
                bRead.close();
            }

        }catch(Exception e) {
            System.out.println("This file does not exists!");
            e.printStackTrace();
        }
    }
    
    public int getScore(){
        int a=(int) (Math.sqrt(15)*random.nextGaussian()+75);
        return a;
    }

    public void WriteInfo(String tar)throws IOException{
        FileWriter o=new FileWriter(tar,true);
        PrintWriter out = new PrintWriter(o);
        if(tar=="orderlist.txt"){
            out.printf("Name,Id,Chinese,Math,Eng,Phy,Chemistry,Sum");
        }

        for(int i =0;i<5;i++){
            out.printf(stuList.get(i).getName()+","+stuList.get(i).getId()+","+stuList.get(i).getChinese()+",");
            out.printf(stuList.get(i).getMath()+","+stuList.get(i).getEng()+","+stuList.get(i).getPhy()+",");
            out.printf(stuList.get(i).getChe()+","+stuList.get(i).getSum());
                           
        }
        out.close();
    }

    public void QuickSort(int l,int r){
        if (l<r){
            int i=l,j=r,x=stuList.get(l).getSum();
            Student stud=stuList.get(l);
            while(i<j){
                while(i<j&&stuList.get(j).getSum()>=x){
                    j--;
                }
                if(i<j){
                    stuList.set(i++,stuList.get(j));
                }
                while(i<j&&stuList.get(i).getSum()<x){
                    i++;
                }
                if(i<j){
                    stuList.set(j--,stuList.get(i));
                }
            }
            stuList.set(i,stud);
            QuickSort(l,i-1);
            QuickSort(i+1,r);
        }
    }

    public void getAver(String aid)throws IOException{
        FileWriter o=new FileWriter(aid,true);
        PrintWriter out = new PrintWriter(o);

        int sum0=0,sum1=0,sum2=0,sum3=0,sum4=0,sum5=0;
        for(int i0: sumOfChin){ sum0+=i0; }
        for(int i1: sumOfMath){ sum1+=i1; }
        for(int i2: sumOfEng){ sum2+=i2; }
        for(int i3: sumOfPhy){ sum3+=i3; }
        for(int i4: sumOfChe){ sum4+=i4; }
        for(int i5: sumOfSum){ sum5+=i5; }

        out.printf("\r\nAver.."+sum0/5+","+sum1/5+","+sum2/5+","+sum3/5+","+sum4/5+","+sum5/5);
        out.close();
    }
}

